package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.service.EduCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api("课程评论前台显示")
@RestController
@RequestMapping("/eduservice/comment")
//@CrossOrigin
public class  CommentFrontController{
    @Autowired
    private EduCommentService commentService;

    @ApiOperation("评论分页查询")
    @GetMapping("/getFrontCommentList/{page}/{limit}")
    public R getFrontCommentList(@PathVariable long page,@PathVariable long limit,String courseId){
        Map<String,Object> map = commentService.getFrontCommentList(page,limit,courseId);
        return R.ok().data(map);
    }

    @ApiOperation("发表评论")
    @PostMapping("/addComment")
    public R addComment(@RequestBody EduComment comment, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)) {
            return R.error().code(28004).message("请登录");
        }
        commentService.addComment(comment);
        return R.ok();
    }

}
